package com.lzw.bluetoothpractice.Activitys;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lzw.bluetoothpractice.Adapters.RecommendDoctorListAdapter;
import com.lzw.bluetoothpractice.Adapters.RecommendHosptialListAdapter;
import com.lzw.bluetoothpractice.AppConfig;
import com.lzw.bluetoothpractice.R;
import com.lzw.bluetoothpractice.Utils.AppUtil;
import com.lzw.bluetoothpractice.Utils.SharePreferenceUtils;
import com.lzw.bluetoothpractice.bean.RecommendDoctorBean;
import com.lzw.bluetoothpractice.bean.RecommendHosptialBean;
import com.lzw.bluetoothpractice.network.HeadAssmblyUtils;
import com.lzw.bluetoothpractice.network.RetrofitManager;
import com.lzw.bluetoothpractice.network.api.RequestApi;

import java.util.Map;

import static com.lzw.bluetoothpractice.MyApplication.getContext;

public class DoctorsOnlineActivity extends BaseActivity {

    private Map<String, String> keyMap;
    private RecommendDoctorListAdapter recommendDoctorListAdapter;
    private RecyclerView rv_recommendDoctor;

    private RecommendHosptialListAdapter recommendHosptialListAdapter;
    private RecyclerView rv_hosptialList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctors_online);
        InitNavBar2(true,"线上问诊",false);
        rv_recommendDoctor=this.findViewById(R.id.rv_doctorList);
        rv_hosptialList=this.findViewById(R.id.rv_hosptialList);
        initDoctor();
        initHosptial();
    }

    private void initDoctor(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/user/recommendDoctor");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;

        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).getDoctor("1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("登录",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                RecommendDoctorBean homeGridBean = JSON.parseObject(json.toString(), RecommendDoctorBean.class);
                if(homeGridBean.getCode()==200){

                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager=new LinearLayoutManager(DoctorsOnlineActivity.this);
                    linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
                    rv_recommendDoctor.setLayoutManager(linearLayoutManager);
                    rv_recommendDoctor.addItemDecoration(new DividerItemDecoration(DoctorsOnlineActivity.this,LinearLayoutManager.HORIZONTAL));
                    //rchealthyList.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
                    recommendDoctorListAdapter=new RecommendDoctorListAdapter(DoctorsOnlineActivity.this);

                    recommendDoctorListAdapter.updateData(homeGridBean.getData());
                    rv_recommendDoctor.setAdapter(recommendDoctorListAdapter);
                }else {
                    Toast toast=Toast.makeText(DoctorsOnlineActivity.this,"查询推荐医生失败失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }

    private void initHosptial(){
        try {
            keyMap = HeadAssmblyUtils.HeadAssembly("/dev-api/android/hospital/recommendHospital");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String accessToken = SharePreferenceUtils.getString(getContext(),"access_token",null);;

        RetrofitManager.getInstance(AppConfig.BASE_URL)
                .createReq(RequestApi.class).recommendHospital("1", AppUtil.getVersionCode() + "", accessToken ,keyMap.get("gmtStr"), keyMap.get("contentMD5"), keyMap.get("hmacCode")).
                observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Subscriber<JSONObject>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

                Log.e("推荐医院：",e.getMessage()+"");

            }

            @Override
            public void onNext(JSONObject json) {

                RecommendHosptialBean hosptialBean = JSON.parseObject(json.toString(), RecommendHosptialBean.class);
                if(hosptialBean.getCode()==200){

                    //Article article=new Article();
                    LinearLayoutManager linearLayoutManager2=new LinearLayoutManager(DoctorsOnlineActivity.this);
                    linearLayoutManager2.setOrientation(LinearLayoutManager.VERTICAL);
                    rv_hosptialList.setLayoutManager(linearLayoutManager2);
                    rv_hosptialList.addItemDecoration(new DividerItemDecoration(DoctorsOnlineActivity.this,LinearLayoutManager.HORIZONTAL));
                    //rchealthyList.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
                    recommendHosptialListAdapter=new RecommendHosptialListAdapter(DoctorsOnlineActivity.this);

                    recommendHosptialListAdapter.updateData(hosptialBean.getData());
                    rv_hosptialList.setAdapter(recommendHosptialListAdapter);
                }else {
                    Toast toast=Toast.makeText(DoctorsOnlineActivity.this,"查询推荐医院失败",Toast.LENGTH_LONG);
                    toast.show();
                }


            }
        });

    }

}